package com.dap.controller;


import com.dap.pojo.Result;
import com.dap.service.ParameterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/Parameter")
public class ParameterController {

    @Autowired
    private ParameterService parameterService;


    @RequestMapping(value = "/initParameter",method = RequestMethod.POST)
    public Result flowControllerParameter(@RequestBody String json){

        //调试代码
        System.out.println("***********************"+json);
        result = parameterService.setParameter(json);


        return result;
    }

    private Result result = new Result();

    /**
     * 参数添加
     * @param key
     * @param filed
     * @param value
     * @return
     */
    @RequestMapping("/hashAdd")
    public Result add(String key, String filed, String value){
        Boolean add = parameterService.add(key, filed, value);
        result.setFlag(add);
        result.setMessage(value);
        return result;
    }

    /**
     * 参数添加
     * @param key
     * @param value
     * @return
     */
    @RequestMapping("/stringAdd")
    public Result add(String key, String value) {
        Boolean add = parameterService.add(key, value);
        result.setFlag(add);
        result.setMessage(value);
        return result;
    }

    /**
     * 参数查询
     * @param key
     * @return
     */
    @RequestMapping("/stringGet")
    public Result get(String key){
        String value = parameterService.get(key);
        result.setMessage(value);
        if("null".equals(value)||value.isEmpty()){
            result.setFlag(false);
        }
        result.setFlag(true);
        return result;

    }

    /**
     * 参数查询
     * @param key
     * @param filed
     * @return
     */
    @RequestMapping("/hashGet")
    public Result get(String key,String filed){
        String value = parameterService.get(key, filed);
        result.setMessage(value);
        if("null".equals(value)||value.isEmpty()){
            result.setFlag(false);
        }
        result.setFlag(true);
        return result;

    }

    /**
     * 参数修改
     * @param key
     * @param filed
     * @param value
     * @return
     */
    @RequestMapping("/hashUpdate")
    public Result update(String key,String filed,String value){
        Boolean update = parameterService.add(key, filed, value);
        result.setFlag(update);
        result.setMessage(value);
        return result;
    }

    /**
     * 参数修改
     * @param key
     * @param value
     * @return
     */
    @RequestMapping("/stringUpdate")
    public Result update(String key,String value){
        Boolean update = parameterService.add(key,value);
        result.setFlag(update);
        result.setMessage(value);
        return result;
    }

    /**
     * 参数删除
     * @param key
     * @return
     */
    @RequestMapping("/del")
    public Result del(String key) {
        Boolean del = parameterService.del(key);
        result.setFlag(del);
        result.setMessage(key);
        return result;
    }
}
